using System.Collections;
using System.Collections.Generic;
using ExitGames.Client.Photon;
using UnityEngine;

public class BCWFlagController : MonoBehaviour, IPunObservable
{
	private List<BCWFlag> listFlags = new List<BCWFlag>();

	public static BCWFlagController instance;

	[HideInInspector]
	public BCWFlag ourPlayerCaptureFlag;

	public int countRedTeam;

	public int countBlueTeam;

	public TDMResult winnerTeam = TDMResult.None;

	private int pointForOneFlag = 1;

	[SerializeField]
	private float timeForPoint = 3f;

	private int maxPoint = 100;

	public int pointForKill = 1;

	[HideInInspector]
	public PhotonView photonView;

	private int myTeam;

	private int enemyTeam;

	private bool showEndWindow;

	private IEnumerator _updateForMaster;

	private IEnumerator _updateForPlayer;

	private bool lastStart;

	private void Awake()
	{
		instance = this;
		photonView = GetComponent<PhotonView>();
	}

	private void OnEnable()
	{
		if (_updateForMaster == null)
		{
			_updateForMaster = UpdateForMaster();
			StartCoroutine(_updateForMaster);
		}
		if (_updateForPlayer == null)
		{
			_updateForPlayer = UpdateForPlayer();
			StartCoroutine(_updateForPlayer);
		}
	}

	private void OnDisable()
	{
		if (_updateForMaster != null)
		{
			StopCoroutine(_updateForMaster);
			_updateForMaster = null;
		}
		if (_updateForPlayer != null)
		{
			StopCoroutine(_updateForPlayer);
			_updateForPlayer = null;
		}
	}

	private void OnDestroy()
	{
		instance = null;
		if (_updateForMaster != null)
		{
			StopCoroutine(_updateForMaster);
			_updateForMaster = null;
		}
		if (_updateForPlayer != null)
		{
			StopCoroutine(_updateForPlayer);
			_updateForPlayer = null;
		}
	}

	private void Update()
	{
	}

	private IEnumerator UpdateForMaster()
	{
		while (true)
		{
			yield return new WaitForSeconds(timeForPoint);
			if (PhotonNetwork.isMasterClient && PVPManager.instance != null && PVPManager.instance.gameIsStarted && TDMController.instance.CountTeam(PunTeams.Team.blue) > 0 && TDMController.instance.CountTeam(PunTeams.Team.red) > 0)
			{
				UpdateCountForMaster();
			}
		}
	}

	private IEnumerator UpdateForPlayer()
	{
		while (true)
		{
			yield return new WaitForSeconds(0.5f);
			CeckCaptureFlag();
		}
	}

	private void CeckCaptureFlag()
	{
		bool flag = false;
		foreach (BCWFlag listFlag in listFlags)
		{
			if (listFlag.iCapturedFlag)
			{
				ourPlayerCaptureFlag = listFlag;
				flag = true;
			}
		}
		if (!flag)
		{
			ourPlayerCaptureFlag = null;
		}
	}

	private void UpdateCountForMaster()
	{
		int num = 0;
		int num2 = 0;
		foreach (BCWFlag listFlag in listFlags)
		{
			if (listFlag.capturedFlag == CapturedTeam.Blue)
			{
				num += pointForOneFlag;
			}
			else if (listFlag.capturedFlag == CapturedTeam.Red)
			{
				num2 += pointForOneFlag;
			}
		}
		if (countRedTeam != maxPoint && countBlueTeam != maxPoint)
		{
			countRedTeam += num2;
			countBlueTeam += num;
		}
		if (countRedTeam >= maxPoint && countBlueTeam >= maxPoint && !showEndWindow)
		{
			showEndWindow = true;
			photonView.RPC("EndRound", PhotonTargets.All, PunTeams.Team.none);
		}
		if (countRedTeam >= maxPoint)
		{
			countRedTeam = maxPoint;
			if (!showEndWindow)
			{
				showEndWindow = true;
				photonView.RPC("EndRound", PhotonTargets.All, PunTeams.Team.red);
			}
		}
		if (countBlueTeam >= maxPoint)
		{
			countBlueTeam = maxPoint;
			if (!showEndWindow)
			{
				showEndWindow = true;
				photonView.RPC("EndRound", PhotonTargets.All, PunTeams.Team.blue);
			}
		}
	}

	public void UpdateInfoForFlag()
	{
		foreach (BCWFlag listFlag in listFlags)
		{
			listFlag.UpdateInfoForFlag();
		}
		FlagCaptured();
	}

	public void FlagCaptured()
	{
		int num = 0;
		int num2 = 0;
		int num3 = 0;
		List<string> list = new List<string>();
		List<string> list2 = new List<string>();
		List<string> list3 = new List<string>();
		foreach (BCWFlag listFlag in listFlags)
		{
			if (listFlag.capturedFlag == CapturedTeam.None)
			{
				num++;
				list3.Add(listFlag._nameFlag);
				continue;
			}
			PunTeams.Team team = ((listFlag.capturedFlag != CapturedTeam.Blue) ? PunTeams.Team.red : PunTeams.Team.blue);
			if (TDMController.myTeam == team)
			{
				num3++;
				list.Add(listFlag._nameFlag);
			}
			else
			{
				num2++;
				list2.Add(listFlag._nameFlag);
			}
		}
		if (GameInfoWindow.instance != null)
		{
			GameInfoWindow.instance.tdmInfoUI.flagGrid.UpdateFlag(num, list3.ToArray(), num3, list.ToArray(), num2, list2.ToArray());
		}
	}

	public void AddFlag(BCWFlag flag)
	{
		listFlags.Add(flag);
		FlagCaptured();
	}

	public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
	{
		if (PVPManager.instance != null)
		{
			if (!lastStart && PVPManager.instance.gameIsStarted && PhotonNetwork.isMasterClient)
			{
				Reset();
			}
			lastStart = PVPManager.instance.gameIsStarted;
		}
		if (PVPManager.instance != null && PVPManager.instance.gameIsStarted)
		{
			if (stream.isWriting)
			{
				stream.SendNext(countBlueTeam);
				stream.SendNext(countRedTeam);
			}
			else
			{
				countBlueTeam = (int)stream.ReceiveNext();
				countRedTeam = (int)stream.ReceiveNext();
			}
		}
	}

	public void Reset()
	{
		countBlueTeam = 0;
		countRedTeam = 0;
		foreach (BCWFlag listFlag in listFlags)
		{
			listFlag.Reset();
			listFlag.ColorArea(CapturedTeam.None, CapturedTeam.None, false);
		}
		showEndWindow = false;
		FlagCaptured();
	}

	[PunRPC]
	public void EndRound(PunTeams.Team winner)
	{
		if (winner == TDMController.myTeam)
		{
			winnerTeam = TDMResult.Winner;
		}
		else if (winner == TDMController.enemyTeam)
		{
			winnerTeam = TDMResult.Loser;
		}
		else if (winner == PunTeams.Team.none)
		{
			winnerTeam = TDMResult.Draw;
		}
		PVPManager.instance.TimeOut();
		switch (winner)
		{
		case PunTeams.Team.none:
			countBlueTeam = maxPoint;
			countRedTeam = maxPoint;
			break;
		case PunTeams.Team.blue:
			countBlueTeam = maxPoint;
			break;
		case PunTeams.Team.red:
			countRedTeam = maxPoint;
			break;
		}
		GameInfoWindow.instance.scrollsTdmScore.UpdateScores();
		Invoke("Reset", 5.5f);
		if (PhotonNetwork.isMasterClient)
		{
			ExitGames.Client.Photon.Hashtable customProperties = PhotonNetwork.room.customProperties;
			if (customProperties.ContainsKey("StartRoundTime"))
			{
				customProperties["StartRoundTime"] = null;
			}
			else
			{
				customProperties.Add("StartRoundTime", null);
			}
			PhotonNetwork.room.SetCustomProperties(customProperties);
		}
	}

	public List<BCWFlag> GetListFlag()
	{
		return listFlags;
	}
}
